package com.bjut.vendormgrsys.support.bo;

import com.bjut.vendormgrsys.support.serialized.CustomAuthorityDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
public class UserBO implements UserDetails {

    private String id;

    /**
     * 账号
     */
    private String account;

    /**
     * 中文名
     */
    private String name;

    /**
     * 手机号
     */
    private String phone;

    /**
     * 电子邮箱
     */
    private String email;

    /**
     * 密码MD5
     */
    private String passwd;

    /**
     * 角色
     */
    private List<RoleBO> role = new ArrayList<>();

    /**
     * 角色名
     */
    private String roleName;

    /**
     * 公司
     */
    private String company;

    /**
     * 状态
     * ENABLE
     * DISABLE
     */
    private String status;

    /**
     * 创建人id
     */
    private String creator;

    /**
     * 创建时间
     */
    private long dateCreated;

    /**
     * 创建时间
     */
    private long lastUpdated;

    @Override
    @JsonDeserialize(using = CustomAuthorityDeserializer.class)
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return role;
    }

    @Override
    public String getPassword() {
        return this.passwd;
    }

    @Override
    public String getUsername() {
        return this.account;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return "ENABLE".equals(status);
    }
}
